package org.NooLab.itexx.app.gui.handlers;

import org.NooLab.itexx.app.gui.CombiPanel;
import org.NooLab.utilities.strings.StringsUtil;



/**
 * 
 * a WebStart demo...
 * http://swingx.java.net/
 * 
 * 
 * 
 * @author kwa
 *
 */
public class PanelText {

	String contentType = "text/html" ;
	
	String text="" ;
	String rawText  = "" ;

	transient CombiPanel combiPanel;

	private StringsUtil strgutil;
	
	// ------------------------------------------------------------------------
	public PanelText(CombiPanel combiPanel){
		this.combiPanel = combiPanel;
		strgutil = combiPanel.getParentObj().strgutil ;
	}
	// ------------------------------------------------------------------------

	public PanelText(PanelText inPT) {
		
		text = inPT.text;
		contentType = inPT.contentType ;
		
		combiPanel = inPT.combiPanel ; 
	}

	public void prepareText(){
		String str;
		int p,f1,f2;

		if (strgutil==null){
			strgutil = combiPanel.getParentObj().strgutil ;
		}
		
		if (text==null){
			text="";
		}
		str = text;
		f1 = strgutil.frequencyOfStr(str, "<");
		f2 = strgutil.frequencyOfStr(str, ">");
		
		str = strgutil.replaceAll(str, "\n\r", "</br>");
		str = strgutil.replaceAll(str, "\n", "</br>");
		
		p = str.toLowerCase().indexOf("html") ;
		if (p<0){
			str = "<html><body>\n" + str + "\n</body></html>" ;
		}
		if (str.contains("(@link")){
			str = createHtmlLinkReferences(str);
		}

		text = str;
	}
	
	
	private String createHtmlLinkReferences(String inStr) {
		String str=inStr;
		if (str.contains("(@link")==false){
			return str;
		}
		
		int p1,p2;
		String tstr0,tstr1,tstr2, linkLabel ;
		String[] linkStr;
		String linkRef ;
		
		while (str.contains("(@link")){
			p1 = str.indexOf("(@link") ;
			p2 = str.indexOf("(@link",p1+2) ;
			linkRef = " ";
			
			if ((p1>=0) && (p2>p1)){
				tstr0 = str.substring(0,p1);
				tstr1 = str.substring(0,p2+7);
				tstr2 = str.substring(p2+7, str.length());
				
				linkStr = strgutil.substringsBetweenEmbracingPair(tstr1, "(@link", "(@link)", 1); // marker is included on left end only
				// linkStr = strgutil.substringsBetweenEmbracingPair(tstr1, "(@link", "(@link)", 0);
				// linkStr = strgutil.substringsBetweenEmbracingPair(tstr1, "(@link", "(@link)", 2); // marker is included on both ends 
				
				if (linkStr.length>0){
					
					linkRef = linkStr[0] ;
					linkRef = linkRef.replace("(@link", "");
					int p= linkRef.indexOf(")");
					
					if (p>=0){
						linkLabel = linkRef.substring(0,p-1).trim();
						linkRef = linkRef.substring(p,linkRef.length()).trim();
					}else{
						linkLabel = linkRef.trim().substring(1, linkRef.length());
					}
					
					
					linkRef = "<a href=\""+ linkRef+"\" target=\"_blank\">"+linkLabel+"</a> ";
				}
				str = tstr0 + linkRef + tstr2; 
			}
			
		}
		
		
		return str;
	}
	
	public void highlightTextByWord(String hiliteThis){
		
		establishHighlightedText(hiliteThis);
		combiPanel.updateText();
	}

	private void establishHighlightedText(String str) {
		
		
	}
	// ----------------------------------------------------
	public void clearText() {
		// TODO Auto-generated method stub
		text="";
		combiPanel.updateText();
	}
	
	public void setText( String string){
		
		text = string ; // combiPanel.getPanelText();
		
		if (contentType.contains("text/html")){
			prepareText();
		}
		text = text.trim();
		combiPanel.updateText();
	}
	

	public String getText() {
		return text;
	}
	public String getContentType() {
		return contentType;
	}


	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

}
